package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.base.mapper.BaseTreeMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseLandMapper;
import com.ruoyi.base.domain.BaseLand;
import com.ruoyi.base.service.IBaseLandService;

/**
 * 地块操作Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-06
 */
@Service
public class BaseLandServiceImpl implements IBaseLandService {
    @Autowired
    private BaseLandMapper baseLandMapper;

    @Autowired
    private BaseTreeMapper baseTreeMapper;

    /**
     * 查询地块操作
     *
     * @param id 地块操作主键
     * @return 地块操作
     */
    @Override
    public BaseLand selectBaseLandById(Long id) {
        return baseLandMapper.selectBaseLandById(id);
    }

    /**
     * 查询地块操作列表
     *
     * @param baseLand 地块操作
     * @return 地块操作
     */
    @Override
    public List<BaseLand> selectBaseLandList(BaseLand baseLand) {
        List<BaseLand> baseLands = baseLandMapper.selectBaseLandList(baseLand);

        for (BaseLand land : baseLands) {
            Long count = baseTreeMapper.selectTreeCountByLandId(land.getId());
            land.setTreeNumber(count);
        }

        return baseLands;
    }

    /**
     * 新增地块操作
     *
     * @param baseLand 地块操作
     * @return 结果
     */
    @Override
    public int insertBaseLand(BaseLand baseLand) {
        baseLand.setCreateTime(DateUtils.getNowDate());
        return baseLandMapper.insertBaseLand(baseLand);
    }

    /**
     * 修改地块操作
     *
     * @param baseLand 地块操作
     * @return 结果
     */
    @Override
    public int updateBaseLand(BaseLand baseLand) {
        baseLand.setUpdateTime(DateUtils.getNowDate());
        return baseLandMapper.updateBaseLand(baseLand);
    }

    /**
     * 批量删除地块操作
     *
     * @param ids 需要删除的地块操作主键
     * @return 结果
     */
    @Override
    public int deleteBaseLandByIds(Long[] ids) {
        for (Long id : ids) {
            baseTreeMapper.deleteByLandId(id);
        }
        return baseLandMapper.deleteBaseLandByIds(ids);
    }

    /**
     * 删除地块操作信息
     *
     * @param id 地块操作主键
     * @return 结果
     */
    @Override
    public int deleteBaseLandById(Long id) {
        baseTreeMapper.deleteByLandId(id);
        return baseLandMapper.deleteBaseLandById(id);
    }
}
